#include <stdio.h>
#include <stdlib.h>

int **matrixReshape(int **nums, int numsSize, int *numsColSize, int r, int c, int *returnSize, int **returnColumnSizes)
{
    int m = numsSize;
    int n = numsColSize[0];
    if (m * n != r * c)
    {
        *returnSize = numsSize;
        *returnColumnSizes = numsColSize;
        return nums;
    }
    *returnSize = r;
    *returnColumnSizes = malloc(sizeof(int) * r);
    int **ans = malloc(sizeof(int *) * r);

    for (int i = 0; i < r; i++)
    {
        (*returnColumnSizes)[i] = c;
        ans[i] = malloc(sizeof(int) * c);
    }
    for (int x = 0; x < m * n; ++x)
    {
        ans[x / c][x % c] = nums[x / n][x % n];
    }
    return ans;
}

int main()
{
    int nums[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int numsSize = 2;
    int numsColSize[2] = {3, 3};
    int r = 1;
    int c = 6;
    int returnSize;
    int *returnColumnSizes;
    int *numsArray[2];
    numsArray[0] = nums[0];
    numsArray[1] = nums[1];
    int **reshapedMatrix = matrixReshape(numsArray, numsSize, numsColSize, r, c, &returnSize, &returnColumnSizes);
    printf("重塑后的矩阵:\n");
    for (int i = 0; i < returnSize; i++)
    {
        for (int j = 0; j < returnColumnSizes[i]; j++)
        {
            printf("%d ", reshapedMatrix[i][j]);
        }
        printf("\n");
    }

    for (int i = 0; i < r; i++)
    {
        free(reshapedMatrix[i]);
    }
    free(reshapedMatrix);
    free(returnColumnSizes);

    return 0;
}